package grape.attend.service.group.impl;

import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import grape.attend.service.classe.api.IClasseService;
import grape.attend.service.classe.po.Classe;
import grape.attend.service.group.api.IGroupService;
import grape.attend.service.group.po.Group;
import grape.attend.service.group.po.GroupClasseRel;
import grape.attend.service.group.mapper.GroupClasseRelMapper;
import grape.attend.service.group.api.IGroupClasseRelService;
import grape.attend.service.user.dto.GroupAndClasseDto;
import grape.attend.service.user.dto.GroupAndClassesDto;
import grape.common.exception.runtime.RBaseException;
import grape.common.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static grape.attend.service.classe.api.IClasseService.isCrossTime;

/**
 * <p>
 * 考勤组与班次关系表，一对多 服务实现类
 * </p>
 *
 * @author yangwei
 * @since 2020-05-08
 */
@Service
public class GroupClasseRelServiceImpl extends BaseServiceImpl<GroupClasseRelMapper, GroupClasseRel> implements IGroupClasseRelService {

    @Autowired
    private IGroupService iGroupService;
    @Autowired
    private IClasseService iClasseService;

    @Override
    public List<GroupAndClasseDto> getGroupAndClasseCurrentByUserId(String userId) {
        List<Group> groups = iGroupService.getGroupByUserId(userId);
        List<GroupAndClasseDto> result = null;
        if (!isEmpty(groups)) {
            result = new ArrayList<>();
            List<Classe> classeList = null;
            for (Group group : groups) {
                classeList = iClasseService.getByGroupId(group.getId());
                if (!isEmpty(classeList)) {
                    for (Classe classe : classeList) {
                        result.add(new GroupAndClasseDto(group, classe));
                    }
                }
            }
            result = result.stream().sorted(new Comparator<GroupAndClasseDto>() {
                @Override
                public int compare(GroupAndClasseDto o1, GroupAndClasseDto o2) {
                    return CompareUtil.compare(o1.getClasse().getStartTime(),o2.getClasse().getStartTime());
                }
            }).collect(Collectors.toList());
        }
        return result;
    }

    @Override
    public List<GroupAndClassesDto> getGroupAndClassesCurrentByUserId(String userId) {
        List<Group> groups = iGroupService.getGroupByUserId(userId);
        List<GroupAndClassesDto> result = null;
        if (!isEmpty(groups)) {
            result = new ArrayList<>();
            List<Classe> classeList = null;
            for (Group group : groups) {
                classeList = iClasseService.getByGroupId(group.getId());
                if (!isEmpty(classeList)) {
                    result.add(new GroupAndClassesDto(group, classeList));
                }
            }
        }
        return result;
    }

    @Override
    public void checkClasse(List<String> groupIds) {
        for (String groupId : groupIds) {
            List<Classe> classes = iClasseService.getByGroupId(groupId);
            List<Classe> crossTime = isCrossTime(classes);
            if (!isEmpty(crossTime)) {
                Group group = iGroupService.getById(groupId);
                throw new RBaseException(StrUtil.format("考勤组 {} 中的班次 {} 和 {} 时间有交集",group.getName(),crossTime.get(0).getName(),crossTime.get(1).getName()));
            }

        }
    }
}
